//
//  CNGasMoneyCaculator.m
//  CarNote
//
//  Created by linshili on 13-10-19.
//  Copyright (c) 2013年 linshili. All rights reserved.
//

#import "CNGasMoneyCaculator.h"

@interface CNGasMoneyCaculator ()

@property (nonatomic) NSMutableArray *pushedFactor;             //小型操作数队列
@property (nonatomic) GasMoneyCaculateFactor resultFactorType;  //保存实时计算的结果类型
@property (nonatomic) float resultFactorValue;                  //实时计算的结果数值

@end

@implementation CNGasMoneyCaculator

- (id)init
{
    self = [super init];
    if (self) {
        _pushedFactor = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)pushCaculateFactor:(GasMoneyCaculateFactor)factor value:(float)value
{
    if ([_pushedFactor count] > 0) {
        NSDictionary *lastPushedFactor = [_pushedFactor lastObject];
        if ([[lastPushedFactor objectForKey:@"factorType"] integerValue] == factor) {
            [_pushedFactor removeLastObject];
        }
        else if ([_pushedFactor count] == 2) {
            [_pushedFactor removeObjectAtIndex:0];
        }

        NSDictionary *needPushFactor = @{@"factorType": [NSNumber numberWithInteger:factor],
                                         @"factorValue": [NSNumber numberWithFloat:value]};
            
        [_pushedFactor addObject:needPushFactor];
        
    }
    else {
        NSDictionary *needPushFactor = @{@"factorType": [NSNumber numberWithInteger:factor],
                                         @"factorValue": [NSNumber numberWithFloat:value]};
        
        [_pushedFactor addObject:needPushFactor];
    }

    if ([_pushedFactor count] == 2) {
        NSDictionary *factorA = [_pushedFactor firstObject];
        NSDictionary *factorB = [_pushedFactor lastObject];
        
        GasMoneyCaculateFactor factorAType = [[factorA objectForKey:@"factorType"] integerValue];
        GasMoneyCaculateFactor factorBType = [[factorB objectForKey:@"factorType"] integerValue];
        
        float factorAValue = [[factorA objectForKey:@"factorValue"] floatValue];
        float factorBValue = [[factorB objectForKey:@"factorValue"] floatValue];
        
        // 判断计算结果类型
        if ((factorAType == GasMoneyCaculateFactorGas && factorBType == GasMoneyCaculateFactorMoney)
            || (factorAType == GasMoneyCaculateFactorMoney && factorBType == GasMoneyCaculateFactorGas))
        {
            _resultFactorType = GasMoneyCaculateFactorPrice;
        }
        
        if ((factorAType == GasMoneyCaculateFactorPrice && factorBType == GasMoneyCaculateFactorMoney)
            || (factorAType == GasMoneyCaculateFactorMoney && factorBType == GasMoneyCaculateFactorPrice))
        {
            _resultFactorType = GasMoneyCaculateFactorGas;
        }
        
        if ((factorAType == GasMoneyCaculateFactorGas && factorBType == GasMoneyCaculateFactorPrice)
            || (factorAType == GasMoneyCaculateFactorPrice && factorBType == GasMoneyCaculateFactorGas))
        {
            _resultFactorType = GasMoneyCaculateFactorMoney;
        }
        
        // 计算结果数值
        if (_resultFactorType == GasMoneyCaculateFactorPrice) {
            if (factorAType == GasMoneyCaculateFactorMoney) {
                _resultFactorValue = [self caculatePriceWithMoney:factorAValue gas:factorBValue];
            }
            else if (factorBType == GasMoneyCaculateFactorMoney) {
                _resultFactorValue = [self caculatePriceWithMoney:factorBValue gas:factorAValue];
            }
        }
        else if (_resultFactorType == GasMoneyCaculateFactorGas) {
            if (factorAType == GasMoneyCaculateFactorMoney) {
                _resultFactorValue = [self caculateGasWithMoney:factorAValue price:factorBValue];
            }
            else if (factorBType == GasMoneyCaculateFactorMoney) {
                _resultFactorValue = [self caculateGasWithMoney:factorBValue price:factorAValue];
            }
        }
        else if (_resultFactorType == GasMoneyCaculateFactorMoney) {
            if (factorAType == GasMoneyCaculateFactorPrice) {
                _resultFactorValue = [self caculateMoneyWithPrice:factorAValue gas:factorBValue];
            }
            else if (factorBType == GasMoneyCaculateFactorPrice){
                _resultFactorValue = [self caculateMoneyWithPrice:factorBValue gas:factorAValue];
            }
        }
        
    }

}


- (GasMoneyCaculateFactor)resultType
{
    return _resultFactorType;
}


- (float)resultValue
{
    return _resultFactorValue;
}


- (float)caculateGasWithMoney:(float)_money price:(float)_price
{
    if (_price == 0) {
        return 0;
    }
    
    NSInteger n = (NSInteger)(_money / _price * 100);
    return (float)n * 0.01;
}


- (float)caculatePriceWithMoney:(float)_money gas:(float)_gas
{
    if (_gas == 0) {
        return 0;
    }
    
    NSInteger n = (NSInteger)(_money / _gas * 100);
    return (float)n * 0.01;
}


- (float)caculateMoneyWithPrice:(float)_price gas:(float)_gas
{
    NSInteger n = (NSInteger)(_price  * _gas * 100);
    return (float)n * 0.01;
}


@end
